package com.nlp.solve.issue_b;


import com.alibaba.fastjson.JSONObject;
import com.nlp.solve.ExportExcel;
import com.nlp.solve.Main;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.*;

@Component
public class Issue_B implements ExportExcel {
    private Logger logger = LoggerFactory.getLogger(Issue_B.class);

    public static void main(String[] args) throws IOException {
//        Issue_B issue_b = new Issue_B();
//        issue_b.Issue_B("F:\\IdeaProjects\\NLP2.0\\src\\test\\tmp\\result.json", "F:\\IdeaProjects\\NLP2.0\\src\\test\\tmp\\issue_b\\");
        HSSFWorkbook workbook = new HSSFWorkbook();
        excel(workbook, new File("./"));
//        System.out.println(new Date().getYear().);
    }

    public String Issue_B(String abs, String outputDir) throws IOException {
        //国家：｛年份：次数｝
        Map<String, Map<String, Integer>> res = new HashMap<>();
        Map<String, Map<String, Map<String, Integer>>> map = JSONObject.parseObject(Main.read(abs), Map.class);
        //国家 ：｛年份：｛学科：次数｝｝
        //国家
        for (Map.Entry<String, Map<String, Map<String, Integer>>> entry : map.entrySet()) {
            Map<String, Map<String, Integer>> value = entry.getValue();
            for (Map.Entry<String, Map<String, Integer>> e : value.entrySet()) {
                if (e.getKey() == null || "null".equals(e.getKey()) || "2021".equals(e.getKey())) {
                    continue;
                }
                Map<String, Integer> map1 = res.computeIfAbsent(entry.getKey(), m -> new TreeMap<>());
                for (Map.Entry<String, Integer> e1 : e.getValue().entrySet()) {
                    map1.put(e.getKey(), map1.getOrDefault(e.getKey(), 0) + e1.getValue());
                }
            }
        }
        Main.write(res, outputDir + "issue_b.json");
        return outputDir + "issue_b.json";
    }

    public static void excel(Workbook workbook, File file) throws IOException {
        String input = "F:\\IdeaProjects\\NLP2.0\\src\\test\\tmp\\issue_b\\issue_b.json";
        Map<String, Map<String, Integer>> map = JSONObject.parseObject(Main.read(input), Map.class);
        Sheet sheet = workbook.createSheet();
        List<String> countries = new ArrayList<>(20);
        Set<String> years = new TreeSet<>();
        for (Map.Entry<String, Map<String, Integer>> entry : map.entrySet()) {
            countries.add(entry.getKey());
        }
        for (Map.Entry<String, Map<String, Integer>> entry : map.entrySet()) {
            for (Map.Entry<String, Integer> e : entry.getValue().entrySet()) {
                years.add(e.getKey());
            }
        }
        Row row0 = sheet.createRow(0);
        int index = 0;
        for (String year : years) {
            row0.createCell(++index).setCellValue(year);
        }
        index = 0;
        Map<String, Integer> yearMap = new TreeMap<>();
        for (String country : countries) {
            Row row = sheet.createRow(++index);
            int i = 0;
            row.createCell(i).setCellValue(country);
            int sum = 0;
            for (String year : years) {
                int count = map.get(country).get(year) == null ? 0 : map.get(country).get(year);
                yearMap.put(year, yearMap.getOrDefault(year, 0) + count);
                sum += count;
                row.createCell(++i).setCellValue(count);
            }
            row.createCell(++i).setCellValue(sum);
        }
        Row row = sheet.createRow(++index);
        index = 0;
        for (Map.Entry<String, Integer> entry : yearMap.entrySet()) {
            row.createCell(++index).setCellValue(entry.getValue());
        }

        try (DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("./issue_b.xlsx")))) {
            workbook.write(dataOutputStream);
        }
    }
}
